package io.gitee.chearnee.fw.sidecar.util;

import io.gitee.chearnee.fw.common.model.vo.ResultVo;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 12/20/2022 10:49 AM
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ResultUtil {


    public static <T> T getResultVo(ResultVo<T> result) {
        return getResultVo(result, true);
    }

    public static <T> T getResultVo(ResultVo<T> result,
                                    Boolean throwException) {
        if (result == null) {
            throw new RuntimeException("sidecar统计服务返回Null");
        }
        if (!Objects.equals(result.getCode(), 200)) {
            if (Boolean.TRUE.equals(throwException)) {
                throw new RuntimeException("sidecar统计服务异常");
            } else {
                log.error("sidecar异常:{}", result.getMessage());
            }
        }
        return result.getData();
    }

}
